home *** CD-ROM | disk | FTP | other *** search
- Path: classic.iinet.com.au!news
- From: ng@mitswa.com.au (John A Ng)
- Newsgroups: comp.lang.c++
- Subject: Re: Dumb Question? about ptr
- Date: Tue, 16 Jan 1996 09:09:37 GMT
- Organization: MITS (WA)
- Message-ID: <4dfq0e$btq@classic.iinet.com.au>
- References: <00001a81+00008b8f@msn.com>
- NNTP-Posting-Host: grunge205.nv.iinet.net.au
- X-Newsreader: Forte Free Agent 1.0.82
-
- Namic@msn.com (The ugly pink sweater book The Orange book is) wrote:
-
- >Dont laugh im only been programming in this for a few hours but why is
- >this giving me the following errors in Line 11 (I copied straight out
- >o a how to program in C++ book. Yes the compilers match i have
- >Borland C++ 4.02
-
- >Error NONAME00.CPP 11: Type name expected
- >Error NONAME00.CPP 11: Variable 'foo' is initialized more than once
- >Error NONAME00.CPP 11: Improper use of typedef 'MyStruct'
- >
-
- >#include <iostream.h>
-
- >class MyStruct {
- > public:
- > int data;
- > int value;
- >};
-
- >MyStruct *foo;
- >MyStruct Record1;
- >foo = &MyStruct; // Line 11
-
- >void main() {
- > foo->data=5;
- > cout << foo->data;
- >};
-
-
- I would like to know the name of the book so I could recommend to people
- not to read it.
-
- Line 11 is an executable statement and it should be written inside a
- function (ie inside main()).
-
- However, this will still not compile since MyStruct is only a definition
- and no object exists. What should be done is:
-
-
-
- you are trying to write an executable statement outside of any function.
- You should instead say:
-
- MyStruct *foo = &MyStruct;
-
- However, this will still not compile because MyStruct is only a
- declaration. There is yet no object. You will need to create a real
- object by instantiating the class declaration as:
-
- MyStruct *foo = new MyStruct;
-
- Regards,
-
- John Ng
- ng@mitswa.com.au
- Western Australia
-
-